#pragma once

#include <atlbase.h>
#include <dshow.h>

#ifndef WM_MEDIA_NOTIFY
 #define WM_MEDIA_NOTIFY		(WM_APP + 1)
#endif

#define VOLUME_FULL     0L
#define VOLUME_SILENCE  -10000L

enum PLAYSTATE {
	Stopped = 0,
	Paused,
	Running,
	Init
};

class CDShowManager
{
protected:
	HWND      ghApp;
	TCHAR     g_szFileName[MAX_PATH];
	BOOL      g_bAudioOnly, g_bFullscreen;
	LONG      g_lVolume;
	DWORD     g_dwGraphRegister;
	PLAYSTATE g_psCurrent;
	double    g_PlaybackRate;

	// DirectShow interfaces
	IGraphBuilder	*pGB;
	IMediaControl	*pMC;
	IMediaEventEx	*pME;
	IVideoWindow	*pVW;
	IBasicAudio		*pBA;
	IBasicVideo		*pBV;
	IMediaSeeking	*pMS;
	IMediaPosition	*pMP;
	IVideoFrameStep	*pFS;

public:
	CDShowManager();
	virtual ~CDShowManager();

	BOOL	SetAudioVolume(long Vol);
	long	GetAudioVolume();
	HRESULT	ToggleFullScreen(void);
	BOOL	IsFullScreen();
	BOOL	GetFrameStepInterface(void);
	HRESULT	StepOneFrame(void);
	HRESULT	StepFrames(int nFramesToStep);
	void	Play();
	void	Pause();
	void	Stop();
	HRESULT	SetRate(double dRate);
	double	GetRate();
	void	OnMediaNotify();
	long	GetPlayDuration();
	long	GetPlayPos();
	BOOL	SetPlayPos(long msec);
	BOOL	GetVideoSize(LONG* pWidth, LONG* pHeight);

protected:
	void	InitDefaultValues();
	void	ReleaseAllInterfaces();

	HRESULT	AddGraphToRot(IUnknown *pUnkGraph, DWORD *pdwRegister);
	void	RemoveGraphFromRot(DWORD pdwRegister);
	void	Msg(TCHAR *szFormat, ...);
	void	CheckVisibility(void);
	HRESULT	InitVideoWindow(int nMultiplier, int nDivider);
	HRESULT PlayMovieInWindow(LPTSTR szFile);
};